package pl.com.hme.kody.pocztowe.adapter;

import java.util.ArrayList;

import pl.com.hme.kody.pocztowe.R;
import pl.com.hme.kody.pocztowe.data.PointOfInterest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ResultAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    protected Context mContext;
    protected ArrayList<PointOfInterest> mList;
    private int mRowViewResourceId = R.layout.list_item_result;

    
    public ResultAdapter(ArrayList <PointOfInterest> list, Context context) {
        mList = list;
        mContext = context;
    }
    
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && position < mList.size()) {
            return mList.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        if (position < mList.size()) {
            return position;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(mRowViewResourceId , null);

            holder = new ViewHolder();
            holder.postalCode = (TextView) convertView.findViewById(R.id.postal_code);
            holder.city = (TextView) convertView.findViewById(R.id.city);
            holder.street = (TextView) convertView.findViewById(R.id.street);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PointOfInterest poi = mList.get(position);
        
        holder.postalCode.setText(poi.getCode());
        holder.city.setText(poi.getLocality());
        holder.street.setText(poi.getStreet() + " " + poi.getStreetNumbers());
        
        
        return convertView;
    }
    
    protected LayoutInflater getLayoutInflater() {
        if (mLayoutInflater == null) {
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        return mLayoutInflater;
    }
    
    private class ViewHolder{
        public TextView postalCode;
        public TextView city;
        public TextView street;
    }
}
